<?php

namespace App\Admin\Controllers;


use App\Admin\Export\OrderGoodsExport;
use App\OrderGoods;

use App\OrderPay;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class OrderGoodsController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('订单销量统计');
            $content->description('订单销量统计');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('订单销量统计');
            $content->description('订单销量统计');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(OrderGoods::class, function (Grid $grid) {
            $grid->model()->join('order_pay', 'order_pay.order_pay_id', '=', 'order_pay_goods.order_pay_id')
                ->where('order_pay.pay_status', 1)
                ->where('order_pay.refund_status', '!=', OrderPay::REFUND_STATUS_APPLIED)
                ->groupBy('good_id')
                ->orderBy('num','desc')
                ->select('good_id', \DB::raw('sum(buy_num) as num'), \DB::raw('sum(buy_num*good_price) as price'), 'good_object');
            //隐藏创建按钮
            $grid->good_object('商品名称')->display(function ($value) {
                return $value['goods_name'];
            });
            $grid->num('商品销售数量')->sortable();
            $grid->price('销售额');
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });

            });

            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->between('create_time', '销售时间')->datetime();

            });

            $grid->exporter(new OrderGoodsExport());
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(OrderGoods::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
